<?php
/**
 *推荐modle
 */
namespace App\Models;

class Phonemap extends Base
{
    protected $table = 'phone_logs';

    const STATUS_1 = 1;
    const STATUS_2 = 0;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">成功</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">失败</span>',
    ];
    const CONNET_STATUS_1 = 0;
    const CONNET_STATUS_2 = 1;
    const CONNET_STATUS_3 = 2;
    const CONNET_STATUS_4 = 3;
    const CONNET_STATUS_TEXT = [
        self::CONNET_STATUS_1 => '<span class="el-tag el-tag--danger">未接通(未接通未扣费)</span>',
        self::CONNET_STATUS_2 => '<span class="el-tag el-tag--danger">振铃30s以上(未接通已扣费)</span>',
        self::CONNET_STATUS_3 => '<span class="el-tag el-tag--warning">接通15s以上(已接通已扣费)</span>',
        self::CONNET_STATUS_4 => '<span class="el-tag el-tag--danger">接通15s以内(已接通未扣费)</span>',
    ];

    protected $fillable = [
        'id','anumber','bnumber','xnumber','response','created_at','updated_at','status','timestamp','connet_status','log_id'
    ];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('anumber','like','%' . $params['kwd'] . '%')
                        ->orwhere('bnumber','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['connet_status']) && $params['connet_status']) {
                    $query ->where('connet_status',$params['connet_status']);
                }
            })->where('status','<>',0)->orderBy('created_at', 'desc')->paginate($limit);
        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'connet_status'=> self::CONNET_STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
        $fields = [ 'created_at','xnumber','connet_status','anumber','bnumber' ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('status','<>',-1)->get();
        foreach ($lists as $k=>$v){
            if($v->attributes['connet_status'] == 0){
                $v->attributes['connet_status'] = '未接通';
            }elseif ($v->attributes['connet_status'] == 1){
                $v->attributes['connet_status'] = '已接通';
            }else{
                $v->attributes['connet_status'] = '通话时间大于15s';
            }
            $v->attributes['aname'] = Users::where('mobile',$v['anumber'])->value('username');
            $v->attributes['bname'] = Users::where('mobile',$v['bnumber'])->value('username');
        }
        return $lists;
    }


}
